<?php

/**
 * [PHPFOX_HEADER]
 */
defined('PHPFOX') or exit('NO DICE!');

class Survey_Service_Process extends Phpfox_Service {

    /**
     * Class constructor
     */
    public function __construct() {
        $this->_sTable = Phpfox::getT('survey');
    }

    public function addSurvey($aVals, $iUpdateId = null) {
        $aForms = array(
            'title' => array(
                'message' => Phpfox::getPhrase('survey.please_enter_title_for_your_question'),
                'type' => array('string:required')
            ),
            'question' => array(
                'message' => Phpfox::getPhrase('survey.please_enter_your_question'),
                'type' => 'string:required'
            ),
        );
        
        $iPrivacy = $aVals["privacy"];
        $sRefType = isset($aVals["attachment-type"]) ? $aVals["attachment-type"] : 'none';
        $sRef = isset($aVals["ref_link"]) ? $aVals["ref_link"] : null;
        $aVals = $this->validator()->process($aForms, $aVals);

        if (!Phpfox_Error::isPassed()) {
            return false;
        }
        $aVals['title'] = $this->preParse()->convert($aVals['title']);
        $aVals['question'] = $this->preParse()->convert($aVals['question']);
        $aVals["privacy"] = $iPrivacy;
        $aVals["time_stamp"] = PHPFOX_TIME;
        $aVals["ref_type"] = $sRefType;
        $aVals["ref_link"] = $sRef;
        $aVals['privacy_comment'] = 0;
        $aVals['user_id'] = PHPFOX::getUserId();
        if ($iUpdateId !== null) {
            $this->database()->update($this->_sTable, $aVals, 'package_id = ' . (int) $iUpdateId);
        } else {

            $iId = $this->database()->insert($this->_sTable, $aVals);
            $oFile = Phpfox::getLib('file');
            $oImage = Phpfox::getLib('image');
            if ($aVals["ref_type"] == "image") {
                foreach ($_FILES['image']['error'] as $iKey => $sError) {
                    if ($sError == UPLOAD_ERR_OK) {
                        if ($aImage = $oFile->load('image[' . $iKey . ']', array(
                            'jpg',
                            'gif',
                            'png'), null)
                        ) {
                            $sFileName = $oFile->upload('image[' . $iKey . ']', Phpfox::getParam('core.dir_pic') . "survey" . PHPFOX_DS, $iId, true);
                            $oImage->createThumbnail(Phpfox::getParam('survey.dir_image') . sprintf($sFileName, ''), Phpfox::getParam('survey.dir_image') . sprintf($sFileName, '_' . 500), 500, 500);
                            // Update image to database
                            $this->database()->update($this->_sTable,array('ref_link' => $sFileName),'survey_id = '.$iId);
                        } else {
                            
                        }
                    }
                }
            }
            (Phpfox::isModule('feed') ? Phpfox::getService('feed.process')->add('survey', $iId, $aVals['privacy'], $aVals['privacy_comment'], 0, PHPFOX::getUserid()) : null);
        }

        return $iId;
    }

    public function createThumbnail($sFileName) {
        $oImage = Phpfox::getLib('image');
        if ($oImage->createThumbnail(Phpfox::getParam('core.dir_pic') . "survey" . PHPFOX_DS . sprintf($sFileName, ''), Phpfox::getParam('core.dir_pic') . "survey" . PHPFOX_DS . sprintf($sFileName, '_446'), 446, 446, true, false)) {
            ;
        }
    }

    public function joinCategory($iCategoryId, $iUserId) {
        $aExist = $this->database()->select("*")
                ->from(Phpfox::getT("survey_following_category"))
                ->where("category_id = $iCategoryId AND user_id = $iUserId")
                ->execute("getRow");
        if (!empty($aExist)) {
            // Unfollow
            $this->database()->delete(Phpfox::getT("survey_following_category"), "category_id = $iCategoryId AND user_id = $iUserId");
        }
        else{
            $this->database()->insert(Phpfox::getT("survey_following_category"), array(
                'category_id' => $iCategoryId,
                'user_id' => $iUserId
            ));
        }
        return true;
    }

    public function follow($iSureyId, $iUserId) {
        $aExist = $this->database()->select("*")
                ->from(Phpfox::getT("survey_following"))
                ->where("survey_id = $iSureyId AND user_id = $iUserId")
                ->execute("getRow");
        if (!empty($aExist)) {
            // Unfollow
            $this->database()->delete(Phpfox::getT("survey_following"), "survey_id = $iSureyId AND user_id = $iUserId");
        }else{
            $this->database()->insert(Phpfox::getT("survey_following"), array(
                'survey_id' => $iSureyId,
                'user_id' => $iUserId
            ));
        }
        return true;
    }

}

?>